package com.wontube.client.extension.io.protocol;

import java.lang.reflect.Type;


import com.google.gson.Gson;
import com.wontube.client.io.IObjectProtocol;

/**
 * @author yuechao
 *
 */
public final class JsonObjectProtocol implements IObjectProtocol {	
	private final Gson gson = new Gson();

	/* 
	 * @see client.io.IObjectProtocol#toProtoString(java.lang.Object)
	 */
	@Override
	public String toProtoString(Object value) {
		if (value == null) {
			return "";
		}

		return this.gson.toJson(value);
	}

	/* 
	 * @see client.io.IObjectProtocol#toObject(java.lang.String)
	 */
	@Override
	public Object toObject(String value, Type type) {
		if (value == null || "".equals(value)) {
			return null;
		}

		return this.gson.fromJson(value, type);
	}
}
